package com.serialcommunication.gui.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

import com.serialcommunication.controller.Coordinator;
import com.serialcommunication.enums.Engine;
import com.serialcommunication.enums.Mode;
import com.serialcommunication.gui.frame.MainFrame;
import com.serialcommunication.interfaces.IAction;
import com.serialcommunication.interfaces.IBaseComponent;
import com.serialcommunication.interfaces.ITransmitComponent;
import com.serialcommunication.listener.MyMouseListener;

import static com.serialcommunication.constants.GuiConfiguration.*;

public class ModeButtonComponent extends BaseComponent implements ITransmitComponent {
	
	private Mode mode;
	private JButton button;
	
	private static final Color colorActive = Color.YELLOW;
	private static final Color colorInactive = Color.gray;
	

	public ModeButtonComponent(Engine pEngine, IAction pAction, Mode pMode, MainFrame pFrame) {
		super(pEngine, pAction, pFrame);
		
		getPanel().setLayout(null);
		mode = pMode;
		button = new JButton(getTitle());
		button.setBackground(colorInactive);
		button.setFont(new Font(Font.DIALOG, Font.PLAIN, 10));
		addComponentsToPanel();
		setSizeOfElements();
		initListener();
		disableComponent();
	}

	public void setSizeOfElements() {
		getPanel().setSize(PANEL_BUTTON_SIZE_3);
		button.setSize(PANEL_BUTTON_SIZE_3);
	}
	
	public void initListener(){
		button.addMouseListener(new MyMouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				transmitData();
			}
		});
	}

	public void addComponentsToPanel() {
		getPanel().add(button);
	}

	@Override
	public void setValue(String pValue) {
		// nothing to do
	}

	@Override
	public void transmitData() {
		Coordinator.newActionTriggered(this);
		getFrame().setModeForEngine(getEngine(), getMode());
		setActive();
	}

	@Override
	public void disableComponent() {
		button.setEnabled(false);
	}

	@Override
	public String getValue() {
		// TODO Auto-generated method stub
		return null;
	}

	public Mode getMode() {
		return mode;
	}

	@Override
	public void setInActive() {
		button.setBackground(colorInactive);
	}
	
	@Override
	public void setActive() {
		button.setBackground(colorActive);
	}

	@Override
	public void enableComponent() {
		button.setEnabled(true);
	}
}
